{
  "cells": [
    {
      "cell_type": "markdown",
      "id": "cell_0",
      "metadata": {},
      "source": [
        "# Quick Start Example - Extracting aspect from a document"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "cell_1",
      "metadata": {},
      "outputs": [],
      "source": [
        "%pip install -U contextgem"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "cell_2",
      "metadata": {},
      "source": [
        "To run the extraction, please provide your LLM details in the ``DocumentLLM(...)`` constructor further below."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "cell_3",
      "metadata": {},
      "outputs": [],
      "source": [
        "# Quick Start Example - Extracting aspect from a document\n",
        "\n",
        "import os\n",
        "\n",
        "from contextgem import Aspect, Document, DocumentLLM\n",
        "\n",
        "\n",
        "# Example document instance\n",
        "# Document content is shortened for brevity\n",
        "doc = Document(\n",
        "    raw_text=(\n",
        "        \"Consultancy Agreement\\n\"\n",
        "        \"This agreement between Company A (Supplier) and Company B (Customer)...\\n\"\n",
        "        \"The term of the agreement is 1 year from the Effective Date...\\n\"\n",
        "        \"The Supplier shall provide consultancy services as described in Annex 2...\\n\"\n",
        "        \"The Customer shall pay the Supplier within 30 calendar days of receiving an invoice...\\n\"\n",
        "        \"This agreement is governed by the laws of Norway...\\n\"\n",
        "    ),\n",
        ")\n",
        "\n",
        "# Define an aspect with optional concept(s), using natural language\n",
        "doc_aspect = Aspect(\n",
        "    name=\"Governing law\",\n",
        "    description=\"Clauses defining the governing law of the agreement\",\n",
        "    reference_depth=\"sentences\",\n",
        ")\n",
        "\n",
        "# Add aspects to the document\n",
        "doc.add_aspects([doc_aspect])\n",
        "# (add more aspects to the document, if needed)\n",
        "\n",
        "# Create an LLM for extraction\n",
        "llm = DocumentLLM(\n",
        "    model=\"openai/gpt-4o-mini\",  # or any other LLM from e.g. Anthropic, etc.\n",
        "    api_key=os.environ.get(\"CONTEXTGEM_OPENAI_API_KEY\"),  # your API key\n",
        ")\n",
        "\n",
        "# Extract information from the document\n",
        "extracted_aspects = llm.extract_aspects_from_document(doc)\n",
        "# or use async version llm.extract_aspects_from_document_async(doc)\n",
        "\n",
        "# Access extracted information\n",
        "print(\"Governing law aspect:\")\n",
        "print(\n",
        "    extracted_aspects[0].extracted_items\n",
        ")  # extracted aspect items with references to sentences\n",
        "# or doc.get_aspect_by_name(\"Governing law\").extracted_items\n"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.10.0"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 5
}